//code for testing jRsa

#include "../lib/jRsa.h"
#include "../lib/exceptions.h"
#include <string>
#include <iostream>
#include <map>
#include <stdlib.h>

/////
#include <cryptopp/rsa.h>
#include <cryptopp/cryptlib.h>


using namespace std;


int main( int argc, char** argv ){
   try{
  
  string message(85, 'a');
  
  //generate the keys
  pair<jRsa::publicKey,jRsa::privateKey> keys;
  keys = jRsa::generateKeys(atoi(argv[1]));
  
  cout<<message<<endl;

  //encrypt the message
  string cipher;
  cipher = jRsa::encrypt(message, keys.first);
  //cout<<cipher<<endl;
  cout<<cipher.size()<<endl;

  //decrypt the message with the private key
  string recovered;
  recovered = jRsa::decrypt(cipher, keys.second);
  cout<<recovered<<endl<<endl;
  

  string message2("HelloWorld"),cipher2,recovered2;
  cout<<message2<<endl;
  
  //convert the public key into a sendable form
  string serial = jRsa::serializePublicKey(keys.first);
  //cout<<serial << endl;;
  
  
  
  
  //generate a public key from the serialized key
  jRsa::publicKey otherKey = jRsa::recoverKey(serial);
  
  //encrypt using the recovered serialized key
  cipher2= jRsa::encrypt(message2,otherKey);
  cout<<cipher2.size()<<endl;
  //cout<<cipher2<<endl;
  
  try{
  //decrypt the message with the private key
  recovered2 = jRsa::decrypt(cipher2, keys.second);
  }catch( BaseException* e ){
      cout << e->what() << endl;
  }
  cout<<recovered2<<endl;
   } catch( exception& e ) {
       cout << e.what() << endl;
   }


  return 0;

}
